package org.mnj07.jms.topic.selector;

import javax.annotation.Resource;
import javax.jms.*;

public class Receiver
{
  @Resource(lookup = "jms/javaee6/ConnectionFactory")
  private static ConnectionFactory connectionFactory;
  @Resource(lookup = "jms/javaee6/Topic")
  private static Topic topic;

  public static void main(String[] args) {

      String selector = "orderAmount < 5 or orderAmount > 7";

      System.out.println("\nStarting receiver with " + selector + "....");

      try {
          // Creates the needed artifacts to connect to the queue
          Connection connection = connectionFactory.createConnection();
          Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
          MessageConsumer consumer = session.createConsumer(topic, selector);
          connection.start();

          // Loops to receive the messages
          System.out.println("\nInfinite loop. Waiting for a message...");
          while (true) {
              TextMessage message = (TextMessage) consumer.receive();
              System.out.println("Message received: " + message.getText());
          }

      } catch (Exception e) {
          e.printStackTrace();
      }
  }
}
